package com.zhijian.medical.util;

import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;

public class RandomStringGenerator {
    private static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER_CASE = UPPER_CASE.toLowerCase(Locale.ROOT);
    private static final String NUMBERS = "0123456789";
    private static final String ALL_CHARACTERS = UPPER_CASE + LOWER_CASE + NUMBERS;
    private static final int ALL_CHARACTERS_LENGTH = ALL_CHARACTERS.length();
    private final Random random;

    public  RandomStringGenerator() {
        this.random = new SecureRandom();
    }

    /**
     * 生成随机字符串
     * @param length
     * @return
     */
    public String generate(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be positive");
        }

        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS_LENGTH)));
        }

        return sb.toString();
    }

    /**
     * 生成随机日期字符串
     * @param startYear
     * @param endYear
     * @return
     */
    public String generateRandomDateString(int startYear, int endYear) {
        Random random = new Random();
        // 计算两个日期之间的天数差
        long daysBetweenYears = ChronoUnit.DAYS.between(LocalDate.of(startYear, 1, 1), LocalDate.of(endYear, 12, 31));
        // 生成随机天数
        long randomDays = random.nextInt((int) daysBetweenYears + 1);
        // 将随机天数加到起始日期上
        LocalDate randomDate = LocalDate.of(startYear, 1, 1).plusDays(randomDays);

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将日期转换为字符串
        return randomDate.format(formatter);
    }

    /**
     * 生成随机手机号码
     * @return
     */
    public String generateRandomPhoneNumber() {
        Random random = new Random();
        // 手机号码前两位
        String prefix = "13456789";
        // 第一位数字，1
        char firstChar = prefix.charAt(random.nextInt(prefix.length()));
        // 第二位数字，3-9
        char secondChar = prefix.charAt(random.nextInt(prefix.length() - 2) + 2);
        // 生成剩余的9位数字
        StringBuilder phoneNumber = new StringBuilder(String.valueOf(firstChar) + String.valueOf(secondChar));
        for (int i = 0; i < 9; i++) {
            phoneNumber.append(random.nextInt(10));
        }
        return phoneNumber.toString();
    }

    /**
     * 生成随机身份证号码
     * @return
     */
    public String generateRandomIDCardNumber() {
        Random random = new Random();
        StringBuilder idCard = new StringBuilder();
        // 前6位为地区代码，这里以北京市东城区为例
        idCard.append("110101");
        // 接下来的8位为出生日期码，格式为YYYYMMDD
        idCard.append(String.format("%tF", new java.util.Date()).substring(2, 10));
        // 接下来的3位为顺序码，随机生成
        idCard.append(String.format("%03d", random.nextInt(1000)));
        // 最后一位为校验码，这里简单使用随机数代替，实际需要根据前17位计算得出
        idCard.append(random.nextInt(10));
        return idCard.toString();
    }

}